package com.kgc.sbt.provider111;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.CartApi;
import com.kgc.sbt.beans.*;
import com.kgc.sbt.mapper.ProductMapper;
import com.kgc.sbt.mapper.ShoppingCartMapper;
import com.kgc.sbt.mapper.UserLoginMapper;
import com.kgc.sbt.utils.ResultBuildUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Created by zzy on 2021/8/18.
 */
@Service
public class CartProvider implements CartApi {

    @Autowired
    private ShoppingCartMapper cartMapper;

    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public UserLogin selectUserById(Integer userId) {

        return userLoginMapper.selectByPrimaryKey(userId);
    }

    @Override
    public ShoppingCart selectCartById(Integer userId, Integer productId) {
        return cartMapper.selectById(userId, productId);
    }


    @Override
    public boolean addShopcart(Integer userId, Integer productId, Integer quantity) {
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setPrice(productMapper.selectByPrimaryKey(productId).getPrice().intValue());
        cart.setChecked(0);
        return cartMapper.insert(cart) > 0;
    }


    @Override
    public List<ShoppingCartForm> selectCartByUserId(Integer userId) {
        return cartMapper.selcetCartByUserId(userId);
    }


    @Override
    public boolean updateCartQuantity(Integer userId, Integer productId, Integer quantity) {
        return cartMapper.updateNumber(userId, productId, quantity) > 0;
    }

    @Override
    public boolean updateCartNumber(Integer userId, Integer productId, Integer quantity) {
        return cartMapper.updateQuantity(userId, productId, quantity) > 0;
    }

    @Override
    public boolean delCartByCarId(Integer userId, Integer productId) {
        return cartMapper.deleteByProductId(userId, productId) > 0;
    }

    @Override
    public boolean delCartByUserId(Integer userId) {
        return cartMapper.deleteByUserId(userId) > 0;
    }

    @Override
    public String checkAllByUserId(Integer userId) {
        Integer checkSum=cartMapper.checkedSum(userId);
        if(checkSum>0){
            cartMapper.cancelCheckall(userId);
            return "所有商品已取消选中";
        }
        cartMapper.checkallByUserId(userId);
        return "所有商品已被选中";
    }

    @Override
    public String checkSomeByCartId(Integer userId, Integer productId) {
        Integer checkNum=cartMapper.checkedNum(userId,productId);

        if (checkNum==1){
            cartMapper.cancelCheck(userId,productId);
            return "该商品已取消选中";
        }
        cartMapper.CheckOne(userId,productId);
        return "该商品已被选中";
    }


}
